package com.ja.openchat.ui.chat.adapter.viewholder;

import android.util.Log;
import android.util.SparseArray;

import com.ja.openchat.R;
import com.zy.openchat.core.manager.TelegramManager;
import com.ja.openchat.ui.chat.annotation.MessageContentType;
import com.zy.openchat.Constants;


public class MessageViewHolderManager {
    private static final String TAG = "MsgViewHolderManager";
    private static MessageViewHolderManager instance = new MessageViewHolderManager();

    private MessageViewHolderManager() {
        init();
    }

    public static MessageViewHolderManager getInstance() {
        return instance;
    }

    private void init() {
        registerMessageViewHolder(AudioMessageContentViewHolder.class, R.layout.conversation_item_audio_send2, R.layout.conversation_item_audio_receive2);
        registerMessageViewHolder(VoiceMessageContentViewHolder.class, R.layout.conversation_item_audio_send2, R.layout.conversation_item_audio_receive2);
        registerMessageViewHolder(ImageMessageContentViewHolder.class, R.layout.conversation_item_image_send, R.layout.conversation_item_image_receive);
        registerMessageViewHolder(AnimationMessageContentViewHolder.class, R.layout.conversation_item_animation_send, R.layout.conversation_item_animation_receive);
        registerMessageViewHolder(TextMessageContentViewHolder.class, R.layout.conversation_item_text_send, R.layout.conversation_item_text_receive);
        registerMessageViewHolder(RedPacketContentViewHolder.class, R.layout.conversation_item_red_packet_send, R.layout.conversation_item_red_packet_receive);
        registerMessageViewHolder(VideoMessageContentViewHolder.class, R.layout.conversation_item_video_send, R.layout.conversation_item_video_send);
        registerMessageViewHolder(GroupPostContentViewHolder.class, R.layout.conversation_item_group_post, R.layout.conversation_item_group_post);
        registerMessageViewHolder(SimpleNotificationMessageContentViewHolder.class, R.layout.conversation_item_notification, R.layout.conversation_item_notification);
        registerMessageViewHolder(CallMessageContentViewHolder.class,R.layout.conversation_item_call_send,R.layout.conversation_item_call_recive);
        registerMessageViewHolder(FileMessageContentViewHolder.class, R.layout.conversation_item_file_send, R.layout.conversation_item_file_receive);
        registerMessageViewHolder(LocationMessageContentViewHolder.class, R.layout.conversation_item_location_send, R.layout.conversation_item_location_receive);
        registerMessageViewHolder(ContactContentViewHolder.class, R.layout.conversation_item_contact_send, R.layout.conversation_item_contact_receive);
        registerMessageViewHolder(TransferMoneyContentViewHolder.class, R.layout.conversation_item_transfer_money_send, R.layout.conversation_item_transfer_money_recive);
    }

    private SparseArray<Class<? extends MessageContentViewHolder>> messageViewHolders = new SparseArray<>();
    private SparseArray<Integer> messageSendLayoutRes = new SparseArray<>();
    private SparseArray<Integer> messageReceiveLayoutRes = new SparseArray<>();

    public void registerMessageViewHolder(Class<? extends MessageContentViewHolder> clazz, int sendLayoutRes, int receiveLayoutRes) {
        MessageContentType contentType = clazz.getAnnotation(MessageContentType.class);
        if (contentType == null) {
            throw new IllegalArgumentException("the message content viewHolder must be annotated with MessageContentType");
        }

        if (sendLayoutRes == 0 && receiveLayoutRes == 0) {
            throw new IllegalArgumentException("must set message content viewHolder layout ");
        }

        TelegramManager.MessageType[] types = contentType.value();
        for (TelegramManager.MessageType type : types) {
            if (messageViewHolders.get(type.ordinal()) == null) {
                messageViewHolders.put(type.ordinal(), clazz);
                messageSendLayoutRes.put(type.ordinal(), sendLayoutRes);
                messageReceiveLayoutRes.put(type.ordinal(), receiveLayoutRes);
            } else {
                Constants.Loge(MessageViewHolderManager.class.getSimpleName(), "re-register message view holder " + clazz.getSimpleName());
            }
        }
    }

    public @androidx.annotation.LayoutRes
    int sendLayoutResId(int messageType) {
        Integer sendLayoutResId = messageSendLayoutRes.get(messageType);
        return sendLayoutResId == null ? R.layout.conversation_item_notification : sendLayoutResId;
    }

    public @androidx.annotation.LayoutRes
    int receiveLayoutResId(int messageType) {
        Integer receiveLayoutResId = messageReceiveLayoutRes.get(messageType);
        return receiveLayoutResId == null ? R.layout.conversation_item_notification : receiveLayoutResId;
    }

    public Class<? extends MessageContentViewHolder> getMessageContentViewHolder(int messageType) {
        Class clazz = messageViewHolders.get(messageType);
        if (clazz == null) {
            Log.d(TAG, "not register messageContentViewHolder for messageType " + messageType + ", fall-back to UnknownMessageContentViewHolder");
            return SimpleNotificationMessageContentViewHolder.class;
        }
        return clazz;
    }
}
